package presentacion.vista;

import accesoDatos.conexion.Conexion;
import accesoDatos.conexion.Peticion;
import accesoDatos.util.Log;
import accesoDatos.vo.Empleado;
import accesoDatos.vo.Pedido;
import accesoDatos.vo.Producto;
import java.awt.Dimension;
import java.awt.Image;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import presentacion.util.Ventana;
import javax.swing.table.DefaultTableModel;

public class VistaMesero extends javax.swing.JFrame {

    private JFrame parent = null;
    private Empleado empleado = null;

    public Empleado getEmpleado() {
        return empleado;
    }
    private DialogoCambiarContraseña cambiarContraseña = null;
    // Panel Menu
    private List<JButton> botones = new ArrayList<JButton>();
    private List<JLabel> labels = new ArrayList<JLabel>();
    private List<Producto> menu = new ArrayList<Producto>();
    // Panel Pedidos
    private List<Pedido> pedidos = new ArrayList<Pedido>();
    private List<JTable> tablaDelPedido = new ArrayList<JTable>();

    public VistaMesero(JFrame parent, Empleado empleado) {
        initComponents();
        this.parent = parent;
        this.empleado = empleado;
        campoNombre.setText(empleado.getNombre());
        campoCargo.setText(empleado.getCargo());
        campoId.setText(empleado.getId().toString());

        Log.setEvento("Cliente", "INFO", "Se Ejecuta Interfaz de Mesero.");
        Ventana.pantallaCompleta(this);
        traerProductos();
        cargarImagenes(panelBebidas);

        this.setVisible(true);
        parent.setVisible(false);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelDatosEmpleado = new javax.swing.JPanel();
        campoNombre = new javax.swing.JTextField();
        nombreEmpleado = new javax.swing.JLabel();
        cargoEmpleado = new javax.swing.JLabel();
        campoCargo = new javax.swing.JTextField();
        idEmpleado = new javax.swing.JLabel();
        campoId = new javax.swing.JTextField();
        panelMenu = new javax.swing.JPanel();
        tituloMenu = new javax.swing.JLabel();
        tabProductos = new javax.swing.JTabbedPane();
        scrollBebidas = new javax.swing.JScrollPane();
        panelBebidas = new javax.swing.JPanel();
        scrollSopas = new javax.swing.JScrollPane();
        panelSopas = new javax.swing.JPanel();
        scrollPlatosFuertes = new javax.swing.JScrollPane();
        panelPlatosFuertes = new javax.swing.JPanel();
        scrollEnsaladas = new javax.swing.JScrollPane();
        PanelEnsaladas = new javax.swing.JPanel();
        scrollAcompañamientos = new javax.swing.JScrollPane();
        panelAcompañamientos = new javax.swing.JPanel();
        scrollPostres = new javax.swing.JScrollPane();
        panelPostres = new javax.swing.JPanel();
        sliderTamañoImagen = new javax.swing.JSlider();
        panelEstado = new javax.swing.JPanel();
        botonCambiarPassword = new javax.swing.JButton();
        botonCerrarSesion = new javax.swing.JButton();
        panelPedidos = new javax.swing.JPanel();
        tituloMesas = new javax.swing.JLabel();
        tabMesas = new javax.swing.JTabbedPane();
        botonBorrarItem = new javax.swing.JButton();
        scrollComentario = new javax.swing.JScrollPane();
        areaComentario = new javax.swing.JTextArea();
        botonNuevoPedido = new javax.swing.JComboBox();
        botonBorrarPedido = new javax.swing.JButton();
        botonEnviaraCocina = new javax.swing.JButton();

        setTitle("Tu Lunch Restaurant Manager - Mesero");
        setMinimumSize(new java.awt.Dimension(1024, 768));
        setResizable(false);
        setUndecorated(true);

        panelDatosEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Empleado", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 2, 12), new java.awt.Color(102, 102, 102)));

        campoNombre.setEditable(false);
        campoNombre.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        nombreEmpleado.setText("Nombre : ");

        cargoEmpleado.setText("Cargo : ");

        campoCargo.setEditable(false);
        campoCargo.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        idEmpleado.setText("ID : ");

        campoId.setEditable(false);
        campoId.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        javax.swing.GroupLayout panelDatosEmpleadoLayout = new javax.swing.GroupLayout(panelDatosEmpleado);
        panelDatosEmpleado.setLayout(panelDatosEmpleadoLayout);
        panelDatosEmpleadoLayout.setHorizontalGroup(
            panelDatosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDatosEmpleadoLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(nombreEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80)
                .addComponent(cargoEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(27, 27, 27)
                .addComponent(campoCargo, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(102, 102, 102)
                .addComponent(idEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(34, 34, 34)
                .addComponent(campoId, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                .addGap(73, 73, 73))
        );
        panelDatosEmpleadoLayout.setVerticalGroup(
            panelDatosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDatosEmpleadoLayout.createSequentialGroup()
                .addGroup(panelDatosEmpleadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoNombre)
                    .addComponent(cargoEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoCargo)
                    .addComponent(idEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoId))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelMenu.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tituloMenu.setFont(new java.awt.Font("Tahoma", 0, 18));
        tituloMenu.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tituloMenu.setText("MENU DEL DIA");
        tituloMenu.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        tabProductos.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        tabProductos.setFont(new java.awt.Font("Tahoma", 0, 18));
        tabProductos.setInheritsPopupMenu(true);
        tabProductos.setName("Postre"); // NOI18N
        tabProductos.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabProductosStateChanged(evt);
            }
        });

        scrollBebidas.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollBebidas.setAutoscrolls(true);
        scrollBebidas.setName("Bebida"); // NOI18N

        panelBebidas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panelBebidas.setAutoscrolls(true);
        panelBebidas.setName("Bebida"); // NOI18N

        javax.swing.GroupLayout panelBebidasLayout = new javax.swing.GroupLayout(panelBebidas);
        panelBebidas.setLayout(panelBebidasLayout);
        panelBebidasLayout.setHorizontalGroup(
            panelBebidasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1034, Short.MAX_VALUE)
        );
        panelBebidasLayout.setVerticalGroup(
            panelBebidasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        scrollBebidas.setViewportView(panelBebidas);

        tabProductos.addTab("Bebidas", scrollBebidas);

        scrollSopas.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollSopas.setName("Sopa"); // NOI18N

        panelSopas.setName("Sopa"); // NOI18N

        javax.swing.GroupLayout panelSopasLayout = new javax.swing.GroupLayout(panelSopas);
        panelSopas.setLayout(panelSopasLayout);
        panelSopasLayout.setHorizontalGroup(
            panelSopasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1036, Short.MAX_VALUE)
        );
        panelSopasLayout.setVerticalGroup(
            panelSopasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        scrollSopas.setViewportView(panelSopas);

        tabProductos.addTab("     Sopas     ", scrollSopas);

        scrollPlatosFuertes.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPlatosFuertes.setName("Plato Fuerte"); // NOI18N

        panelPlatosFuertes.setName("Plato Fuerte"); // NOI18N

        javax.swing.GroupLayout panelPlatosFuertesLayout = new javax.swing.GroupLayout(panelPlatosFuertes);
        panelPlatosFuertes.setLayout(panelPlatosFuertesLayout);
        panelPlatosFuertesLayout.setHorizontalGroup(
            panelPlatosFuertesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1036, Short.MAX_VALUE)
        );
        panelPlatosFuertesLayout.setVerticalGroup(
            panelPlatosFuertesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        scrollPlatosFuertes.setViewportView(panelPlatosFuertes);

        tabProductos.addTab("      Platos     ", scrollPlatosFuertes);

        scrollEnsaladas.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollEnsaladas.setName("Ensalada"); // NOI18N

        PanelEnsaladas.setName("Ensalada"); // NOI18N

        javax.swing.GroupLayout PanelEnsaladasLayout = new javax.swing.GroupLayout(PanelEnsaladas);
        PanelEnsaladas.setLayout(PanelEnsaladasLayout);
        PanelEnsaladasLayout.setHorizontalGroup(
            PanelEnsaladasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1036, Short.MAX_VALUE)
        );
        PanelEnsaladasLayout.setVerticalGroup(
            PanelEnsaladasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        scrollEnsaladas.setViewportView(PanelEnsaladas);

        tabProductos.addTab("  Ensaladas  ", scrollEnsaladas);

        scrollAcompañamientos.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollAcompañamientos.setName("Acompañamiento"); // NOI18N

        panelAcompañamientos.setName("Acompañamiento"); // NOI18N

        javax.swing.GroupLayout panelAcompañamientosLayout = new javax.swing.GroupLayout(panelAcompañamientos);
        panelAcompañamientos.setLayout(panelAcompañamientosLayout);
        panelAcompañamientosLayout.setHorizontalGroup(
            panelAcompañamientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1036, Short.MAX_VALUE)
        );
        panelAcompañamientosLayout.setVerticalGroup(
            panelAcompañamientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        scrollAcompañamientos.setViewportView(panelAcompañamientos);

        tabProductos.addTab(" Acompañamientos ", scrollAcompañamientos);

        scrollPostres.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPostres.setName("Postre"); // NOI18N

        panelPostres.setName("Postre"); // NOI18N

        javax.swing.GroupLayout panelPostresLayout = new javax.swing.GroupLayout(panelPostres);
        panelPostres.setLayout(panelPostresLayout);
        panelPostresLayout.setHorizontalGroup(
            panelPostresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1036, Short.MAX_VALUE)
        );
        panelPostresLayout.setVerticalGroup(
            panelPostresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        scrollPostres.setViewportView(panelPostres);

        tabProductos.addTab("    Postres    ", scrollPostres);

        tabProductos.setSelectedComponent(scrollBebidas);

        sliderTamañoImagen.setMaximum(200);
        sliderTamañoImagen.setMinimum(60);
        sliderTamañoImagen.setValue(130);
        sliderTamañoImagen.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderTamañoImagenStateChanged(evt);
            }
        });

        javax.swing.GroupLayout panelMenuLayout = new javax.swing.GroupLayout(panelMenu);
        panelMenu.setLayout(panelMenuLayout);
        panelMenuLayout.setHorizontalGroup(
            panelMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelMenuLayout.createSequentialGroup()
                .addGroup(panelMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelMenuLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tabProductos, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE))
                    .addGroup(panelMenuLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(tituloMenu, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelMenuLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(sliderTamañoImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        panelMenuLayout.setVerticalGroup(
            panelMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMenuLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tituloMenu)
                .addGap(18, 18, 18)
                .addComponent(tabProductos, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(sliderTamañoImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        botonCambiarPassword.setFont(new java.awt.Font("Tahoma", 0, 18));
        botonCambiarPassword.setText("Cambiar mi contraseña");
        botonCambiarPassword.setFocusPainted(false);
        botonCambiarPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCambiarPasswordActionPerformed(evt);
            }
        });

        botonCerrarSesion.setFont(new java.awt.Font("Tahoma", 0, 18));
        botonCerrarSesion.setText("Cerrar sesion");
        botonCerrarSesion.setFocusPainted(false);
        botonCerrarSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarSesionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelEstadoLayout = new javax.swing.GroupLayout(panelEstado);
        panelEstado.setLayout(panelEstadoLayout);
        panelEstadoLayout.setHorizontalGroup(
            panelEstadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelEstadoLayout.createSequentialGroup()
                .addContainerGap(568, Short.MAX_VALUE)
                .addComponent(botonCambiarPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botonCerrarSesion)
                .addGap(2, 2, 2))
        );

        panelEstadoLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonCambiarPassword, botonCerrarSesion});

        panelEstadoLayout.setVerticalGroup(
            panelEstadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEstadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelEstadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrarSesion, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonCambiarPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        panelPedidos.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tituloMesas.setFont(new java.awt.Font("Tahoma", 0, 18));
        tituloMesas.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tituloMesas.setText("MESAS A CARGO");

        tabMesas.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        tabMesas.setFont(new java.awt.Font("Tahoma", 0, 18));
        tabMesas.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabMesasStateChanged(evt);
            }
        });

        botonBorrarItem.setFont(new java.awt.Font("Tahoma", 0, 14));
        botonBorrarItem.setText("Quitar Item");
        botonBorrarItem.setEnabled(false);
        botonBorrarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarItemActionPerformed(evt);
            }
        });

        areaComentario.setColumns(20);
        areaComentario.setRows(5);
        areaComentario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                areaComentarioMouseClicked(evt);
            }
        });
        scrollComentario.setViewportView(areaComentario);

        botonNuevoPedido.setFont(new java.awt.Font("Tahoma", 0, 24));
        botonNuevoPedido.setMaximumRowCount(21);
        botonNuevoPedido.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mesa", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));
        botonNuevoPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoPedidoActionPerformed(evt);
            }
        });

        botonBorrarPedido.setFont(new java.awt.Font("Tahoma", 0, 14));
        botonBorrarPedido.setText("Borrar Pedido");
        botonBorrarPedido.setEnabled(false);
        botonBorrarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarPedidoActionPerformed(evt);
            }
        });

        botonEnviaraCocina.setFont(new java.awt.Font("Tahoma", 0, 14));
        botonEnviaraCocina.setText("Enviar a Cocina");
        botonEnviaraCocina.setEnabled(false);
        botonEnviaraCocina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEnviaraCocinaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelPedidosLayout = new javax.swing.GroupLayout(panelPedidos);
        panelPedidos.setLayout(panelPedidosLayout);
        panelPedidosLayout.setHorizontalGroup(
            panelPedidosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPedidosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPedidosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollComentario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .addComponent(botonBorrarItem, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .addComponent(tabMesas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .addComponent(tituloMesas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelPedidosLayout.createSequentialGroup()
                        .addComponent(botonNuevoPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botonBorrarPedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(botonEnviaraCocina)))
                .addContainerGap())
        );
        panelPedidosLayout.setVerticalGroup(
            panelPedidosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPedidosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tituloMesas)
                .addGap(18, 18, 18)
                .addComponent(tabMesas, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botonBorrarItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scrollComentario, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPedidosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(panelPedidosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonBorrarPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(botonEnviaraCocina, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(botonNuevoPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelDatosEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelEstado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(panelMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(panelPedidos, javax.swing.GroupLayout.PREFERRED_SIZE, 418, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelPedidos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelDatosEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1024)/2, (screenSize.height-756)/2, 1024, 756);
    }// </editor-fold>//GEN-END:initComponents
    private void traerProductos() {
        if (Conexion.abrirConexion()) {
            menu = (List<Producto>) Conexion.enviarPeticion(new Peticion(empleado, 2));
            Log.setEvento("Cliente", "INFO", "Recepcion Exitosa");
            Conexion.cerrarConexion();
        }
    }

    private void cargarImagenes(JPanel panel) {
        pack();
        int anchoDelPadre = panel.getParent().getWidth();
        panel.setPreferredSize(new Dimension(anchoDelPadre, 1));
        botones.removeAll(botones);
        panel.removeAll();

        int espacioEntreBotones = 20;
        int tamañoDeBoton = sliderTamañoImagen.getValue();
        int margenIcono = tamañoDeBoton / 5;
        int tamañoDeIcono = tamañoDeBoton - margenIcono;

        int localizacion = espacioEntreBotones + tamañoDeBoton;
        int filas = 0;
        int columnas = 0;

        String tipo = panel.getName();

        for (int i = 0; i < menu.size(); i++) {
            if (menu.get(i).getTipo().equals(tipo) && menu.get(i).getEstaEnMenu()) {

                JButton botonProducto = new JButton();
                JLabel labelProducto = new JLabel();

                // Propiedades del boton
                botonProducto.setSize(tamañoDeBoton, tamañoDeBoton);
                botonProducto.setLocation(localizacion * columnas + espacioEntreBotones, localizacion * filas + espacioEntreBotones);
                botonProducto.setName(String.valueOf(menu.get(i).getId()));
                botonProducto.setToolTipText(menu.get(i).getNombre());
                botonProducto.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        adicionarProductoAPedido(evt);
                    }
                });

                // Propiedades del label
                labelProducto.setSize(tamañoDeBoton, 15);
                labelProducto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                labelProducto.setText(menu.get(i).getNombre());
                labelProducto.setLocation(localizacion * columnas + espacioEntreBotones, localizacion * filas + localizacion);

                // Imagen del boton
                ImageIcon imagenOriginal = menu.get(i).getFoto();
                if (imagenOriginal.getIconHeight() > imagenOriginal.getIconWidth()) {
                    botonProducto.setIcon(new ImageIcon(imagenOriginal.getImage().getScaledInstance(-1, tamañoDeIcono, Image.SCALE_DEFAULT)));
                } else {
                    botonProducto.setIcon(new ImageIcon(imagenOriginal.getImage().getScaledInstance(tamañoDeIcono, -1, Image.SCALE_DEFAULT)));
                }

                // Ubicacion del proximo boton
                if ((columnas + 2) * (localizacion) + espacioEntreBotones >= panel.getWidth()) {
                    filas++;
                    columnas = 0;
                } else {
                    columnas++;
                }

                // Adicion del boton
                botones.add(botonProducto);
                labels.add(labelProducto);
                panel.add(botones.get(botones.size() - 1));
                panel.add(labels.get(labels.size() - 1));

                //Ampliacion del panel contenedor
                pack();
                panel.setPreferredSize(new Dimension(anchoDelPadre, (int) (botonProducto.getLocation().getY() + tamañoDeBoton + espacioEntreBotones)));
                panel.updateUI();
            }
        }
        // Refresco
        pack();
        panel.updateUI();
    }

    private DefaultTableModel modeloTabla() {
        DefaultTableModel modelo = new DefaultTableModel(
                null,
                new String[]{"ID", "Tipo", "Nombre"});
        return modelo;
    }

    private void adicionarProductoAPedido(java.awt.event.ActionEvent evt) {
        JButton boton = (JButton) evt.getSource();
        int ID = Integer.parseInt(boton.getName());

        JScrollPane scrollPane = (JScrollPane) tabMesas.getSelectedComponent();
        if (scrollPane == null) {
            return;
        }

        JViewport viewPort = scrollPane.getViewport();
        JTable tabla = (JTable) viewPort.getComponent(0);
        DefaultTableModel modelo = (DefaultTableModel) tabla.getModel();

        for (int i = 0; i < menu.size(); i++) {
            if (menu.get(i).getId() == ID) {
                Producto producto = menu.get(i);
                String id = String.valueOf(producto.getId());
                String tipo = producto.getTipo();
                String nombre = producto.getNombre();
                modelo.addRow(new String[]{id, tipo, nombre});
            }
        }
        tabla.setRowSelectionInterval(tabla.getRowCount() - 1, tabla.getRowCount() - 1);
        botonEnviaraCocina.setEnabled(true);
        botonBorrarItem.setEnabled(true);
    }

    private void botonCambiarPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCambiarPasswordActionPerformed
        if (cambiarContraseña == null) {
            cambiarContraseña = new DialogoCambiarContraseña(this, true);
        }
        cambiarContraseña.resetAll();
        cambiarContraseña.setEmpleado(empleado);
        cambiarContraseña.pack();
        cambiarContraseña.setVisible(true);
    }//GEN-LAST:event_botonCambiarPasswordActionPerformed

    private void sliderTamañoImagenStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderTamañoImagenStateChanged
        tabProductosStateChanged(evt);
}//GEN-LAST:event_sliderTamañoImagenStateChanged

    private void tabProductosStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabProductosStateChanged
        JScrollPane scrollActual = (JScrollPane) tabProductos.getSelectedComponent();
        JPanel panelActual = (JPanel) scrollActual.getViewport().getComponent(0);
        cargarImagenes(panelActual);
}//GEN-LAST:event_tabProductosStateChanged

    private void botonBorrarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarPedidoActionPerformed
        for (int i = 0; i < pedidos.size(); i++) {
            if (pedidos.get(i).getMesa() == Integer.parseInt(tabMesas.getSelectedComponent().getName())) {
                pedidos.remove(i);
            }
        }
        tabMesas.remove(tabMesas.getSelectedComponent());
        if (tabMesas.getComponentCount() == 0) {
            botonEnviaraCocina.setEnabled(false);
            botonBorrarPedido.setEnabled(false);
        }
    }//GEN-LAST:event_botonBorrarPedidoActionPerformed

    private void botonNuevoPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoPedidoActionPerformed
        int mesa = botonNuevoPedido.getSelectedIndex();
        if (mesa == 0) {
            return;
        }
        
        mesa = Integer.parseInt((String)botonNuevoPedido.getSelectedItem());
        SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        String fecha = formato.format(new Date());
        pedidos.add(new Pedido(mesa, empleado, fecha));

        JTable tabla = new JTable(modeloTabla());
        tablaDelPedido.add(tabla);
        tabla.setName("" + mesa);
        tabla.setRowHeight(40);
        tabla.setAutoscrolls(true);
        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(tabla);
        scroll.setName(String.valueOf(mesa));
        tabMesas.add("" + mesa, scroll);
        tabMesas.setSelectedComponent(scroll);
        botonBorrarPedido.setEnabled(true);
        botonNuevoPedido.removeItem(String.valueOf(mesa));
        botonNuevoPedido.setSelectedIndex(0);
}//GEN-LAST:event_botonNuevoPedidoActionPerformed

    private void botonCerrarSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarSesionActionPerformed
        parent.setVisible(true);
        this.setVisible(false);
        Log.setEvento("Cliente", "INFO", "Se Cierra Interfaz de Mesero.");
        Log.setEvento("Cliente", "INFO", "Se Ejecuta Interfaz de Iniciar Sesion.");
    }//GEN-LAST:event_botonCerrarSesionActionPerformed

    private void botonBorrarItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarItemActionPerformed
        JScrollPane scrollPane = (JScrollPane) tabMesas.getSelectedComponent();
        if (scrollPane != null) {
            JViewport viewPort = scrollPane.getViewport();
            JTable tabla = (JTable) viewPort.getComponent(0);
            DefaultTableModel modelo = (DefaultTableModel) tabla.getModel();
            int selectedRow = tabla.getSelectedRow();
            if (tabla.getRowCount() >= 1) {
                modelo.removeRow(selectedRow);
            }
            if (tabla.getRowCount() == 0) {
                botonBorrarItem.setEnabled(false);
                botonEnviaraCocina.setEnabled(false);
            }
            if (tabla.getRowCount() >= 1) {
                if (tabla.getRowCount() != selectedRow) {
                    tabla.setRowSelectionInterval(selectedRow, selectedRow);
                } else {
                    tabla.setRowSelectionInterval(selectedRow - 1, selectedRow - 1);
                }
            }
        }
}//GEN-LAST:event_botonBorrarItemActionPerformed

    private void areaComentarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_areaComentarioMouseClicked
        TecladoEnPantalla.mostrar();
    }//GEN-LAST:event_areaComentarioMouseClicked

    private void botonEnviaraCocinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEnviaraCocinaActionPerformed
        int mesa = Integer.parseInt(tabMesas.getSelectedComponent().getName());
        IniciarSesion.getInstance().nuevoPedido(mesa);
        for (int i = 0; i < pedidos.size(); i++) {
            if (pedidos.get(i).getMesa() == mesa) {
                pedidos.remove(i);
            }
        }
        tabMesas.remove(tabMesas.getSelectedComponent());
    }//GEN-LAST:event_botonEnviaraCocinaActionPerformed

    private void tabMesasStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabMesasStateChanged
        JScrollPane scrollActual = (JScrollPane) tabMesas.getSelectedComponent();
        if (scrollActual != null) {
            JTable tablaActual = (JTable) scrollActual.getViewport().getComponent(0);
            if (tablaActual.getRowCount() > 0) {
                botonEnviaraCocina.setEnabled(true);
                botonBorrarItem.setEnabled(true);
            } else {
                botonEnviaraCocina.setEnabled(false);
                botonBorrarItem.setEnabled(false);
            }
            System.out.println("hola");
        }
    }//GEN-LAST:event_tabMesasStateChanged

    public static void ejecutar(final JFrame parent, final Empleado empleado) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VistaMesero(parent, empleado);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelEnsaladas;
    private javax.swing.JTextArea areaComentario;
    private javax.swing.JButton botonBorrarItem;
    private javax.swing.JButton botonBorrarPedido;
    private javax.swing.JButton botonCambiarPassword;
    private javax.swing.JButton botonCerrarSesion;
    private javax.swing.JButton botonEnviaraCocina;
    private javax.swing.JComboBox botonNuevoPedido;
    private javax.swing.JTextField campoCargo;
    private javax.swing.JTextField campoId;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JLabel cargoEmpleado;
    private javax.swing.JLabel idEmpleado;
    private javax.swing.JLabel nombreEmpleado;
    private javax.swing.JPanel panelAcompañamientos;
    private javax.swing.JPanel panelBebidas;
    private javax.swing.JPanel panelDatosEmpleado;
    private javax.swing.JPanel panelEstado;
    private javax.swing.JPanel panelMenu;
    private javax.swing.JPanel panelPedidos;
    private javax.swing.JPanel panelPlatosFuertes;
    private javax.swing.JPanel panelPostres;
    private javax.swing.JPanel panelSopas;
    private javax.swing.JScrollPane scrollAcompañamientos;
    private javax.swing.JScrollPane scrollBebidas;
    private javax.swing.JScrollPane scrollComentario;
    private javax.swing.JScrollPane scrollEnsaladas;
    private javax.swing.JScrollPane scrollPlatosFuertes;
    private javax.swing.JScrollPane scrollPostres;
    private javax.swing.JScrollPane scrollSopas;
    private javax.swing.JSlider sliderTamañoImagen;
    private javax.swing.JTabbedPane tabMesas;
    private javax.swing.JTabbedPane tabProductos;
    private javax.swing.JLabel tituloMenu;
    private javax.swing.JLabel tituloMesas;
    // End of variables declaration//GEN-END:variables
}
